package com.briup.demo.controller;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.briup.demo.bean.CmsArticle;
import com.briup.demo.common.Response;
import com.briup.demo.service.CmsArticleService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = "文章管理服务")
@RestController
@RequestMapping("/article")
public class CmsArticleController {
	
	@Autowired
	private CmsArticleService articleService;
	
	@ApiOperation(value = "审核文章")
	@ApiImplicitParams({
		@ApiImplicitParam(name="artId",value = "文章编号"),
		@ApiImplicitParam(name = "state",value = "状态",
							allowableValues = "已发布,已驳回")
	})
	
	@PutMapping(value = "/updateState")
	public Response<String> updateState(String artId,
			String state){
		boolean b = articleService.updateState(artId, state);
		if(b) return Response.ok("操作成功！");
		else return Response.error("要更新的文章不存在！");
	}
			
	@ApiOperation(value = "按照条件检索文章信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "begin",value = "开始时间"),
		@ApiImplicitParam(name = "end",value = "结束时间"),
		@ApiImplicitParam(name = "key",value = "文章标题或者作者")
	})
	@GetMapping("/searchBy")
	public Response<List<CmsArticle>> searchBy(
			Date begin,Date end,String key){
		System.out.println("begin:"+begin);
		System.out.println("end:"+end);
		List<CmsArticle> as = 
				articleService.searchBy(begin, end, key);
		return Response.ok(as);
	}

	
	@ApiOperation(value = "保存或者更新文章信息" , 
			notes = "如果给定文章编号，那么代表更新文章信息<br/>"
					+ "如果没有给定文章编号，那么代表新增文章信息")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "id",
				value = "文章编号,更新时必须传递"),
		@ApiImplicitParam(name = "title",value = "文章标题"),
		@ApiImplicitParam(name = "author",value = "文章作者"),
		@ApiImplicitParam(name = "categoryId",value = "所属栏目编号"),
	})
	@PostMapping("/saveOrUpdate")
	public Response<String> saveOrUpdateArticle(
			String id,String title,String author,String categoryId){
		boolean b = 
			articleService.saveOrUpdate(id, title, author, categoryId);
		if(b) return Response.ok("操作成功！");
		else return Response.error("操作失败！");
	}
	
	@ApiOperation(value = "删除文章")
	@DeleteMapping("/deleteById")
	public Response<String> deleteById(String id){
		int num = articleService.deleteById(id);
		if(num==0){
			return Response.ok("删除失败");
		}
		else{
			return Response.ok("删除成功");
		}
	}
	
	@ApiOperation(value = "批量删除文章")
	@DeleteMapping("/deleteByIds")
	public Response<String> deleteByIds(String[] ids){ 
		int num = articleService.deleteByIds(ids);
		System.out.println("删除了"+num+"篇文章");
		return Response.ok("删除成功");
	}
	
	@ApiOperation(value = "显示所有文章")
	@GetMapping("/selectAll")
	public Response<List<CmsArticle>> selectAll(){
		List<CmsArticle> ca = articleService.selectAll();
		return Response.ok(ca);
	}
}
